<template>
    <rx-dialog @handOk="handleSubmit" @cancel="cancel">
        <rx-layout>
            <div slot="center">
                <a-tabs v-model="activeKey" class="rxTabs">
                    <a-tab-pane v-for="pane in panes" :tab="pane.title" :key="pane.key" forceRender>
                        <div class="rxContent">
                            <form-solution-mapping-pane :ref="'pane_'+pane.key" :conf="pane.conf"/>
                        </div>
                    </a-tab-pane>
                </a-tabs>
            </div>
        </rx-layout>
    </rx-dialog>
</template>

<script>
    import {RxDialog,RxLayout,Util} from "@lowcode/jpaas-common-lib";
    import FormSolutionMappingPane from "./FormSolutionMappingPane";
    import FormBoAttrApi from "@/api/form/bo/formBoAttr";
    export default {
        name: "FormSolutionMappingConfig",
        components:{
            RxDialog,
            RxLayout,
            FormSolutionMappingPane
        },
        props:["layerid","destroy","formConfig","boDefId","oldBoDefId"],
        data(){
            return {
                activeKey:"",
                panes:[]
            }
        },
        created() {
            this.init();
        },
        methods:{
            i18n(name,text,key){
                return this.$ti18(name,text,"formSolution",key);
            },
            init(){
                if(this.formConfig){
                   this.panes=JSON.parse(this.formConfig);
                    for(var i=0;i<this.panes.length;i++) {
                        var obj = this.panes[i];
                        if (i == 0) {
                            this.activeKey = obj.key;
                        }
                    }
                   return;
                }
                FormBoAttrApi.queryToBoEntity(this.boDefId).then(res=>{
                    for(var i=0;i<res.length;i++){
                        var obj=res[i];
                        if(i==0){
                            this.activeKey=obj.tableName;
                        }
                        this.panes.push({
                            title:obj.tableName+this.i18n('msg.mapping','映射'),
                            key:obj.tableName,
                            conf:{dsName: obj.dsAlias,relationType:obj.boRelation.type, boDefId:this.oldBoDefId, tableName: obj.tableName}
                        })
                    }
                });
            },
            handleSubmit(){
                Util.closeWindow(this,"ok",this.getData());
            },
            cancel(){
                Util.closeWindow(this,"cancel");
            },
            getData(){
                for(var i=0;i<this.panes.length;i++){
                    var pane=this.panes[i];
                    pane.conf=this.$refs['pane_' + pane.key][0].getData();
                }
                return JSON.stringify(this.panes);
            }
        }
    }
</script>

<style scoped>

</style>